//
//  DXUtil.m
//  suremodv
//
//  Created by dd on 13-9-17.
//  Copyright (c) 2013年 xiaomuzhi. All rights reserved.
//

#import "DXUtil.h"
#import "DXData.h"
#import <CommonCrypto/CommonDigest.h>

@implementation DX (Util)

+ (NSURL *) urlOfImg:(NSString *)imageName
{
    return [NSURL URLWithString:[NSString stringWithFormat:@"%@/express/img/%@", IMG_HOST, imageName]];
}

+ (NSURL *) urlOfClientImg:(NSString *)imageName
{
    return [NSURL URLWithString:[NSString stringWithFormat:@"%@/upload/client/img/%@", HOST, imageName]];
}

+ (void) alertWithTitle:(NSString *)title message:(NSString *)message delegate:(id<UIPickerViewDelegate>) delegate
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:delegate cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alertView show];
}

+ (void) alert:(NSString *)message delegate:(id<UIPickerViewDelegate>) delegate
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:message delegate:delegate cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alertView show];
}

+ (void) alert:(NSString *)message
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alertView show];
}





+ (NSString *) time
{
    NSDate *now = [NSDate date];
    return [NSString stringWithFormat:@"%d", (int)[now timeIntervalSince1970]];
}

+ (NSString *) timeFormat:(NSString *)time
{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time.integerValue];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    return [dateFormatter stringFromDate:date];
}

+ (NSString *) timeFormat:(NSString *)time format:(NSString *)format
{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time.integerValue];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:format];
    return [dateFormatter stringFromDate:date];
}

+ (NSString *) integer2String:(int)i
{
    return [NSString stringWithFormat:@"%d", i];
}

+ (NSNumber *) integer2Number:(int)i
{
    return [NSNumber numberWithInt:i];
}

+ (UIColor *) colorWithRed:(int)red green:(int)green blue:(int)blue alpha:(float)alpha
{
    return [UIColor colorWithRed:red / 255.0f green:green / 255.0f blue:blue / 255.0f alpha:alpha];
}

+ (UIColor *) colorWithHex:(NSString *)hexColor
{
    unsigned int red, green, blue;
    
    [[NSScanner scannerWithString:[hexColor substringWithRange:NSMakeRange(0, 2)]]scanHexInt:&red];
    [[NSScanner scannerWithString:[hexColor substringWithRange:NSMakeRange(2, 2)]]scanHexInt:&green];
    [[NSScanner scannerWithString:[hexColor substringWithRange:NSMakeRange(4, 2)]]scanHexInt:&blue];
    
    return [UIColor colorWithRed:red / 255.0f green:green / 255.0f blue:blue / 255.0f alpha:1.0f];
}

+ (NSString *)makeRandString:(int)length
{
    NSString *sourceStr = @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    NSMutableString *resultStr = [[NSMutableString alloc] init];
    srand(time(0));
    for (int i = 0; i < length; i++)
    {
        unsigned index = rand() % [sourceStr length];
        NSString *oneStr = [sourceStr substringWithRange:NSMakeRange(index, 1)];
        [resultStr appendString:oneStr];
    }
    return resultStr;
}

+ (NSString *)makePaySN
{
    return [NSString stringWithFormat:@"MP1%@%@", [DX timeFormat:[DX time] format:@"yyyyMMddHHmmss"], [DX makeRandString:9]];
}

+ (NSString *) md5:(NSString *)content
{
    const char *cStr = [content UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5( cStr, strlen(cStr), digest );
    
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02X", digest[i]];
    
    return output;
}



+ (double) screenHeight
{
    return [[UIScreen mainScreen] bounds].size.height;
}

+ (double) screenWidth
{
    return [[UIScreen mainScreen] bounds].size.width;
}



+ (NSString *) IPAddress
{
    NSString *address = @"error";
    struct ifaddrs *interfaces = NULL;
    struct ifaddrs *temp_addr = NULL;
    int success = 0;
    // retrieve the current interfaces - returns 0 on success
    success = getifaddrs(&interfaces);
    if (success == 0) {
        // Loop through linked list of interfaces
        temp_addr = interfaces;
        while(temp_addr != NULL) {
            if(temp_addr->ifa_addr->sa_family == AF_INET) {
                // Check if interface is en0 which is the wifi connection on the iPhone
                if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
                    // Get NSString from C String
                    address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
                }
            }
            temp_addr = temp_addr->ifa_next;
        }
    }
    // Free memory
    freeifaddrs(interfaces);
    return address;
    
}











+ (NSString *)jsonEncode:(id)object
{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object options:0 error:&error];
    if (error != nil || jsonData == nil)
    {
        [DX i:@"json encode error: %@", error];
        return nil;
    }
    
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    return jsonString;
}

+ (id)jsonDecode:(NSString *)jsonString
{
    NSError *error = nil;
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    id object = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
    if (error != nil || object == nil)
    {
        [DX i:@"json decode error: %@", error];
        return nil;
    }
    
    return object;
}


+ (NSInteger) parseInteger:(id)content
{
    if (![content isKindOfClass:[NSNumber class]] && ! [content isKindOfClass:[NSString class]])
    {
        return 0;
    }
    NSString *number = [NSString stringWithFormat:@"%@", content];
    return number.integerValue;
}

+ (float) parseFloat:(id)content
{
    if (![content isKindOfClass:[NSNumber class]] && ! [content isKindOfClass:[NSString class]])
    {
        return 0;
    }
    NSString *number = [NSString stringWithFormat:@"%@", content];
    return number.floatValue;
}

+ (double) parseDouble:(id)content
{
    if (![content isKindOfClass:[NSNumber class]] && ! [content isKindOfClass:[NSString class]])
    {
        return 0;
    }
    NSString *number = [NSString stringWithFormat:@"%@", content];
    return number.floatValue;
}

+ (BOOL)isNull:(id)object
{
    if (object == nil) return YES;
    return [object isKindOfClass:[NSNull class]];
}


@end
